package com.o2o.cleaning.month.platform.ebusiness_plat.intime

import org.apache.spark.SparkContext
import org.apache.spark.sql.SparkSession
import org.elasticsearch.spark._

object _01_ES_TO_OSS {


  def main(args: Array[String]): Unit = {

    val spark = SparkSession.builder()
      .appName("CheckDataDetail")
      .config("spark.debug.maxToStringFields", "2000")
      .config("spark.serializer", "org.apache.spark.serializer.KryoSerializer")
      .config("spark.sql.caseSensitive", "true")
      .config("es.nodes", "192.168.1.29")
      .config("es.port", "9200")
      .config("cluster.name", "O2OElastic")
      .config("es.net.http.auth.user", "elastic")
      .config("es.net.http.auth.pass", "changeme")
      .master("local[*]")
      .getOrCreate()

    val sc = spark.sparkContext
    sc.hadoopConfiguration.set("fs.s3a.access.key", "GAO7EO9FWKPJ8WFCQDME")
    sc.hadoopConfiguration.set("fs.s3a.secret.key", "LZ0xaHBSYKHaJ9ECDbX9f7zin79UZkXfGoNapRPL")
    sc.hadoopConfiguration.set("fs.s3a.endpoint", "https://obs.cn-north-1.myhuaweicloud.com")
    sc.setLogLevel("WARN")

    val year = 2021
    val month = 12


    // 这个需要每个月跑一下,改日期run
    orcToJson(spark, sc, year, month)
    fun(spark, sc, year, month)

    //    spark.read.orc(s"s3a://o2o-dataproces-group/zyf/intime/mongosource/${year}/${month}/").show(false)

  }

  def fun(spark: SparkSession, sc: SparkContext, year: Int, month: Int) {
    val index = s"${year}_intime/intime_${year}_${month}"
    println("----------" + index + "----------")
    val data = sc.esJsonRDD(index).values
    spark.read.json(data).repartition(1).write.orc(s"s3a://o2o-dataproces-group/zyf/intime/essource/${year}/${month}/")
  }

  def orcToJson(spark: SparkSession, sc: SparkContext, year: Int, month: Int): Unit = {
    println(s"${year}---${month}")
    val frame = spark.read.json(s"s3a://o2o-sourcedata-${year}/obs-source-${year}/${month}/intime/intime_${year}_${month}/")
      .repartition(1).write.orc(s"s3a://o2o-dataproces-group/zyf/intime/mongosource/${year}/${month}/")
  }
}
